<?php

namespace app\organization\controller\organization;

use app\common\controller\Frontend;
use app\common\enums\OrganizationStateEnum;
use app\common\model\banxi\organization\Organization as OrganizationModel;
use app\common\model\banxi\organization\OrganizationLevelConfig;
use think\db\exception\DbException;
use think\exception\ValidateException;
use think\facade\Validate;
use think\validate\ValidateRule;

/**
 * 机构
 */
class Organization extends Frontend
{
    protected array $noNeedLogin = [];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new OrganizationModel();
    }

    /**
     * 分页查询
     * @return void
     * @throws DbException
     */
    public function listPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);
        $organizationId = $this->auth->organizationId;
        $param['query']['NE_status'] = 1;
        $param['query']['EQ_parentId'] = $organizationId;
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $list = $this->model->getListPage($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 分页Select查询
     * @return void
     * @throws DbException
     */
    public function listSelectPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);
        if (!empty($param['searchText'])) {
            $param['query']['EQ_name'] = $param['searchText'];
        }
        //查询当前机构和下级机构
        $organizationId = $this->auth->organizationId;
        $param['query']["IN_id"] = $this->model->getSonOrganization($organizationId);
        $param['query']['NE_status'] = OrganizationStateEnum::E1;
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $list = $this->model->getListSelectPage($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', $list);
    }

    /**
     * 分页Select查询
     * @return void
     * @throws DbException
     */
    public function listLevelSelectPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query", 'searchText']);
        //获取分页数据
        $pageParams = get_page_params($param);
        $organizationId = $this->auth->organizationId;
        $param['query']["ids"] = $this->model->getSonOrganization($organizationId);
        $list = $this->model->getListLevelSelectPage($param, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', $list);
    }


    /**
     * 查询
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get()
    {
        $param = $this->request->param(["query" => []]);
        if (empty($param['query'])) {
            $this->fail('请传递你要查询的记录');
        }
        $where = get_query($param);
        $info = $this->model->getInfo($where);
        if (empty($info)) {
            $this->fail('记录不存在');
        }
        $this->ok('操作成功', $info);
    }


    /**
     * 新增或修改
     * @return void
     */
    public function saveOrUpdate()
    {
        $param = $this->request->param();
        try {
            $validate = Validate::rule([
                'id' => ValidateRule::isRequire(null, '请选择你要修改的机构'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());

            $organizationId = $this->auth->organizationId;
            $this->model->saveOrUpdate($param, $organizationId);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功', [], self::NOTIFICATION);
    }

    /**
     * 启停用
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function enable()
    {
        $param = $this->request->param(['id' => 0]);
        try {
            $validate = Validate::rule([
                'id' => ValidateRule::isRequire(null, '请选择你要修改的机构'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
            $this->model->updateEnable($param['id'], $this->auth->organizationId);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok();
    }


    public function getLevelConfig()
    {
        $param = $this->request->param(['organizationId' => 0]);
        try {
            $validate = Validate::rule([
                'organizationId' => ValidateRule::isRequire(null, '请选择你要查看的机构'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
            $organization = $this->model->where(['id' => $param['organizationId']])->find();
            $organizationLevelConfig = new OrganizationLevelConfig();
            $info = $organizationLevelConfig->where(['id' => $organization['level']])->find();
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功', $info);

    }

    public function getMyLevelConfig()
    {
        try {
            $organizationId = $this->auth->organizationId;
            $organization = $this->model->where(['id' => $organizationId])->find();
            $organizationLevelConfig = new OrganizationLevelConfig();
            $info = $organizationLevelConfig->where(['id' => $organization['level']])->find();
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功', $info);
    }
}